package com.ch2;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class ClientHander extends ChannelInboundHandlerAdapter{

	private int counter;
	private byte[] req;
	private final ByteBuf firstMessage;

	public ClientHander() {

		req = ("QUERY TIME ORDER" + System.getProperty("line.separator"))
				.getBytes();
		firstMessage = Unpooled.buffer(req.length);
		firstMessage.writeBytes(req);

	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) {
		ctx.writeAndFlush(Unpooled.copiedBuffer(">>>>>Netty rocks!",
				CharsetUtil.UTF_8));
	}

	protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg)
			throws Exception {
		
		ByteBuf message = null;
		for (int i = 0; i < 100; i++) {
			message = Unpooled.buffer(req.length);
			message.writeBytes(req);
			ctx.writeAndFlush(message);
		}

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();
		ctx.close();
	}

	
}
